'use strict';
(function(){
  const MAP = {
    // Genres
    "电子": "科技",
    "流行": "轻快",
    "嘻哈": "律动",
    "摇滚": "金属",
    "原声": "原声",
    "爵士": "律动",
    "氛围": "安静",
    "国风": "温暖",
    "电影配乐": "史诗",
    "Lo-Fi": "Lo‑Fi",
    "轻音乐": "轻音乐",
    "金属": "金属",
    // Moods
    "治愈": "治愈",
    "温暖": "温暖",
    "轻快": "轻快",
    "悬疑": "悬疑",
    "宏大": "史诗",
    "动感": "律动",
    "科技": "科技",
    "空灵": "清新",
    "感性": "感性",
    "热血": "燃",
    "暗黑": "暗黑"
  };
  function cnLabelFrom(el){
    const text = (el.textContent||'').trim();
    // Prefer the first CJK token before a space or English
    const m = text.match(/^[\u4e00-\u9fa5A-Za-z0-9·\-\u2011\u2013\u2014]+/);
    if(!m) return text;
    // If contains a space, take the part before the space
    const base = m[0];
    const cn = base.split(' ')[0];
    return cn;
  }
  function rewrite(){
    document.querySelectorAll('#genre-grid a.tile, #mood-grid a.tile, #instrument-grid a.tile, #scene-grid a.tile').forEach(a => {
      const label = cnLabelFrom(a);
      const key = MAP[label] || label;
      a.setAttribute('href', 'playlist.html?q=' + encodeURIComponent(key));
    });
  }
  if(document.readyState === 'loading'){
    document.addEventListener('DOMContentLoaded', rewrite);
  }else{
    rewrite();
  }
})();
